﻿using System;
using System.IO;
using System.Text;


namespace MinConsole.Gaf.IO
{
    
    /// <summary>
    /// Summary for the Files class
    /// <source>Subsonic 2.1 final</source>
    /// </summary>
    public static class FileUtility
    {
        /// <summary>
        /// Read a text file and obtain it's contents.
        /// </summary>
        /// <param name="absolutePath">The complete file path to write to.</param>
        /// <returns>String containing the content of the file.</returns>
        public static string GetFileText(string absolutePath)
        {
            using (StreamReader sr = new StreamReader(absolutePath))
                return sr.ReadToEnd();
        }

        /// <summary>
        /// Creates or opens a file for writing and writes text to it.
        /// </summary>
        /// <param name="absolutePath">The complete file path to write to.</param>
        /// <param name="fileText">A String containing text to be written to the file.</param>
        public static void CreateToFile(string absolutePath, string fileText)
        {
            using (StreamWriter sw = File.CreateText(absolutePath))
                sw.Write(fileText);
        }

        /// <summary>
        /// Update text within a file by replacing a substring within the file.
        /// </summary>
        /// <param name="absolutePath">The complete file path to write to.</param>
        /// <param name="lookFor">A String to be replaced.</param>
        /// <param name="replaceWith">A String to replace all occurrences of lookFor.</param>
        public static void UpdateFileText(string absolutePath, string lookFor, string replaceWith)
        {
            string newText = GetFileText(absolutePath).Replace(lookFor, replaceWith);
            WriteToFile(absolutePath, newText);
        }

        /// <summary>
        /// Writes out a string to a file.
        /// </summary>
        /// <param name="absolutePath">The complete file path to write to.</param>
        /// <para0m name="fileText">A String containing text to be written to the file.</param>
        public static void WriteToFile(string absolutePath, string fileText)
        {
            using (StreamWriter sw = new StreamWriter(absolutePath, false))
            {
                sw.Write(fileText);
                sw.Flush(); 
                     
            } //eof using 
        }


        /// <summary>
        /// Appends a text to file
        /// </summary>
        /// <param name="absolutePath">The complete file path to write to </param>
        /// <param name="txt">The txt to append to the File </param>
        public static void AppendToFile(string absolutePath , string txt) 
        {
            using (StreamWriter sw = File.AppendText(absolutePath))
            {
                sw.WriteLine(txt);
                sw.Flush(); 
            } //eof using 
        } //eof method 


        /// <summary>
        /// Creates a default inifile in a format hostName.processName.ini , in the same directory where the 
        /// </summary>
        /// <returns>the file path to the init file in a string </returns>
        public static string CreateProcessDefaultIniFile()
        {
            string strIniFilePath = String.Empty;
            string strPathToExe = Environment.GetCommandLineArgs()[0];
            string extension = Path.GetExtension(strPathToExe);
            //string filename = Path.GetFileName(strPathToExe);
            string strBaseDir = Path.GetDirectoryName(strPathToExe);
            string strMachineName = System.Environment.MachineName;
            string fileNameNoExtension = Path.GetFileNameWithoutExtension(strPathToExe);
            //now build the default ini file path 
            strIniFilePath = strBaseDir + @"\" + strMachineName + "." + fileNameNoExtension + ".ini";

            return strIniFilePath;
        } //eof method 


        /// <summary>
        /// Creates a default a default log file format 
        /// </summary>
        /// <returns>the file path to the init file in a string </returns>
        public static string CreateProcessDefaultLogFile()
        {
            string strIniFilePath = String.Empty;
            string strPathToExe = Environment.GetCommandLineArgs()[0];
            string extension = Path.GetExtension(strPathToExe);
            //string filename = Path.GetFileName(strPathToExe);
            string strBaseDir = Path.GetDirectoryName(strPathToExe);
            string strMachineName = System.Environment.MachineName;
            string fileNameNoExtension = Path.GetFileNameWithoutExtension(strPathToExe);
            //now build the default ini file path 
            string strDatePattern = DateTime.Now.ToString("yyyy.MM.dd");
            strIniFilePath = strBaseDir + @"\" + strMachineName + "." + fileNameNoExtension + strDatePattern + ".log";

            return strIniFilePath;
        } //eof method 






    } //eof clas 
} //eof namespace 
